package com.timesheet.service.employee.impl.dao;

import java.util.List;

import com.timesheet.server.core.dao.BaseDao;
import com.timesheet.server.core.mapper.Mapper;
import com.timesheet.server.core.models.AssignProjectEmployee;
import com.timesheet.server.core.models.AssignProjectEmployeePk;
import com.timesheet.server.core.models.Employee;
import com.timesheet.server.core.models.Project;
import com.timesheet.serverice.employee.interfaces.IEmployeeDao;
import com.timesheet.serverice.interfaces.employee.model.IEmployee;
import com.timesheet.serverice.interfaces.employee.model.IEmployeeRoles;
import com.timesheet.serverice.interfaces.employee.model.IProject;
import com.timesheet.serverice.interfaces.employee.model.ITask;

public class EmployeeDao extends BaseDao implements IEmployeeDao {

	public boolean createEmployee(IEmployee employee) {

		Mapper<Employee> mapper = new Mapper<Employee>();
		Employee emp = new Employee();
		try {
			Employee emp1 = mapper.map(emp, employee);
			persist(emp1);
		} catch (IllegalArgumentException | IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		

		return true;
	}

	public List<IEmployee> getAllEmployees() {
		/* EntityManager entityManager = getEntityManager(); */
		String query = "select emp from Employee emp";

		List<IEmployee> allObjects = getAllObjects(query);
		return getAllObjects(query);

	}

	public boolean assignEmployeeToProject(List<IEmployee> employees,
			IProject project, IEmployeeRoles employeeRoles) {
		begin();

		Project project1 = find(Project.class, project.getProjectId());

		if (project1 == null)
			return false;

		for (IEmployee employee : employees) {

			IEmployee emp = find(IEmployee.class, employee.getEmployeeId());
			if (emp != null) {

				AssignProjectEmployeePk assEmployeePk = new AssignProjectEmployeePk();
				assEmployeePk.setEmployeeId(emp.getEmployeeId());
				assEmployeePk.setProjectId(project1.getProjectId());

				AssignProjectEmployee assignProjectEmployee = new AssignProjectEmployee();
				assignProjectEmployee.setAssignProjectEmployeePk(assEmployeePk);
				persist(assignProjectEmployee);
				assignProjectEmployee.setEmployeeRoles(employeeRoles
						.getRoleId());
			}
		}

		commitAndClose();
		return true;
	}

	public boolean assignTaskToEmployee(IEmployee employee, ITask task,
			IProject project) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

}
